/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgBuscarCliente.java
 *
 * Created on Oct 23, 2011, 8:59:50 PM
 */

package visual.Despacho;

import bentity.beCliente;
import blogic.blCliente;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import visual.Seguridad.visualHelper;

/**
 *
 * @author renzo
 */
public class dlgBuscarCliente extends javax.swing.JDialog {
    private beCliente clienteElegido = null;
    /** Creates new form dlgBuscarCliente */
    public dlgBuscarCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.consultarClientesPorFiltros();
        visualHelper.centrarDialogoEnPantalla(this);
    }

    private void consultarClientesPorFiltros(){
        try{
            blCliente bc = new blCliente();
            ArrayList<beCliente> clientes = bc.obtenerClientes("", "", "", "", "", "");
            DefaultTableModel dtm = (DefaultTableModel)this.tblClientes.getModel();
            this.limpiarTabla(dtm);
            if (clientes.size() == 0)
                JOptionPane.showMessageDialog(this, "La búsqueda no arrojó resultados.", "Información", JOptionPane.INFORMATION_MESSAGE);
            else
                this.llenarTabla(clientes);
        }
        catch (Exception e){
            e.printStackTrace();
            visualHelper.mostrarMensajeError("Ocurrió un error en la búsqueda");
        }
    }

    private void llenarTabla(ArrayList<beCliente> clientes) {
        DefaultTableModel dtm = (DefaultTableModel)this.tblClientes.getModel();
        Object [] datos = new Object [8];
        for(int i=0;i<clientes.size();i++){
            datos[0] = clientes.get(i).getIdCliente();
            datos[1] = clientes.get(i).getRazonSocial();
            datos[2] = clientes.get(i).getRuc();
            datos[3] = clientes.get(i).getDireccion();
            dtm.addRow(datos);
        }
        this.tblClientes.setRowSelectionAllowed(true);
        this.tblClientes.setColumnSelectionAllowed(false);
    }

    private void limpiarTabla(DefaultTableModel dtm) {
        int numFilas = dtm.getRowCount();
        for(int i=(numFilas-1);i>=0;i--)
            dtm.removeRow(i);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitulo = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblClientes = new javax.swing.JTable();
        lblClientes = new javax.swing.JLabel();
        btnSeleccionar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(550, 400));
        setModal(true);
        setResizable(false);
        getContentPane().setLayout(null);

        lblTitulo.setFont(new java.awt.Font("DejaVu Sans", 0, 36));
        lblTitulo.setForeground(new java.awt.Color(0, 0, 0));
        lblTitulo.setText("Buscar cliente");
        getContentPane().add(lblTitulo);
        lblTitulo.setBounds(140, 10, 290, 43);

        tblClientes.setAutoCreateRowSorter(true);
        tblClientes.setForeground(new java.awt.Color(0, 0, 0));
        tblClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Razon social", "RUC", "Dirección"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblClientes.setColumnSelectionAllowed(true);
        tblClientes.setGridColor(javax.swing.UIManager.getDefaults().getColor("TableHeader.background"));
        tblClientes.setMaximumSize(new java.awt.Dimension(800, 72));
        tblClientes.setMinimumSize(new java.awt.Dimension(200, 72));
        tblClientes.setSelectionForeground(new java.awt.Color(0, 0, 0));
        tblClientes.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tblClientes);
        tblClientes.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(40, 90, 470, 230);

        lblClientes.setFont(new java.awt.Font("Dialog", 0, 14));
        lblClientes.setForeground(new java.awt.Color(0, 0, 0));
        lblClientes.setText("Clientes");
        getContentPane().add(lblClientes);
        lblClientes.setBounds(40, 60, 80, 17);

        btnSeleccionar.setBackground(new java.awt.Color(240, 235, 226));
        btnSeleccionar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnSeleccionar.setForeground(new java.awt.Color(0, 0, 0));
        btnSeleccionar.setText("Elegir");
        btnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarActionPerformed(evt);
            }
        });
        getContentPane().add(btnSeleccionar);
        btnSeleccionar.setBounds(140, 330, 120, 27);

        btnCancelar.setBackground(new java.awt.Color(240, 235, 226));
        btnCancelar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnCancelar.setForeground(new java.awt.Color(0, 0, 0));
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar);
        btnCancelar.setBounds(280, 330, 120, 27);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.setVisible(false);
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarActionPerformed
        // TODO add your handling code here:
        int fila = this.tblClientes.getSelectedRow();
        if (fila == -1)
            JOptionPane.showMessageDialog(this, "Debe seleccionar un cliente.", "Información", JOptionPane.INFORMATION_MESSAGE);
        else{
            this.clienteElegido = new beCliente(Integer.parseInt(this.tblClientes.getValueAt(fila, 0).toString()), this.tblClientes.getValueAt(fila, 1).toString(), this.tblClientes.getValueAt(fila,2).toString(), null, null, null, null, beCliente.C_ACTIVO, this.tblClientes.getValueAt(fila, 3).toString());
            this.setVisible(false);
        }
    }//GEN-LAST:event_btnSeleccionarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgBuscarCliente dialog = new dlgBuscarCliente(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSeleccionar;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblClientes;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JTable tblClientes;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the clienteElegido
     */
    public beCliente getClienteElegido() {
        return clienteElegido;
    }

    /**
     * @param clienteElegido the clienteElegido to set
     */
    public void setClienteElegido(beCliente clienteElegido) {
        this.clienteElegido = clienteElegido;
    }

}
